package org.bonorsoft.modules.control.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.bonorsoft.common.system.query.QueryGenerator;
import org.bonorsoft.common.system.vo.LoginUser;
import org.bonorsoft.common.util.oConvertUtils;
import org.bonorsoft.modules.wechat.entity.WxPicture;
import org.bonorsoft.modules.wechat.service.IPicturePhotoService;
import org.bonorsoft.modules.wechat.service.IWxPictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * pc端轮播图上传修改
 */
@Slf4j
@Controller
@RequestMapping("/control/picture")
public class PictureController extends BonorController<WxPicture, IWxPictureService> {
    private String prefix = "control/picture";

    @Autowired
    private IWxPictureService wxPictureService;
    @Autowired
    private IPicturePhotoService picturePhotoService;

    /**
     * 轮播图首页
     * @return
     */
    @RequiresPermissions("control:picture:view")
    @GetMapping()
    public String index() {
        return prefix + "/picture";
    }


    /**
     * 分页列表查询
     * @param wxPicture
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "呼应-分页列表查询")
    @RequiresPermissions("control:picture:list")
    @GetMapping(value = "/list")
    @ResponseBody
    public Result<?> queryPageList(WxPicture wxPicture,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<WxPicture> queryWrapper = QueryGenerator.initQueryWrapper(wxPicture, req.getParameterMap());
        Page<WxPicture> page = new Page<WxPicture>(pageNo, pageSize);
        IPage<WxPicture> pageList = wxPictureService.paging(page,queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 轮播图 添加页面
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 轮播图 添加
     *
     * @param wxPicture
     * @return
     */
    @AutoLog(value = "呼应-添加")
    @RequiresPermissions("control:picture:add")
    @PostMapping(value = "/add")
    @ResponseBody
    public Result<?> add(@Validated WxPicture wxPicture, String photo) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();//取得当前用户
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        wxPicture.setId(uuid).setCreateBy(user.getRealname()).setCreateTime(new Date());
        wxPictureService.save(wxPicture);
        if(oConvertUtils.isNotEmpty(photo)){
            picturePhotoService.addPhoto(uuid,photo);
        }
        return Result.ok("添加成功！");
    }

    /**
     * 轮播图 详细
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, ModelMap map){
        map.put("picture",wxPictureService.getById(id));
        map.put("photo",picturePhotoService.getPhoto(id));
        return prefix + "/detail";
    }

    /**
     * 通过id删除
     */
    @AutoLog(value = "呼应-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        wxPictureService.removeById(id);
        picturePhotoService.deletePhoto(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     */
    @AutoLog(value = "呼应-批量删除")
    @RequiresPermissions("control:picture:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    @ResponseBody
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids){
        List<String> list = Arrays.asList(ids.split(","));
        wxPictureService.removeByIds(list);
        picturePhotoService.deletePhotoByIds(list);
        return Result.ok("删除成功!");
    }

    /**
     * 编辑页面
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap map) {
        WxPicture wxPicture = wxPictureService.getById(id);
        map.put("picture", wxPicture);
        map.put("photo", picturePhotoService.getPhoto(id));
        return prefix + "/edit";
    }

    /**
     * 编辑
     * @param wxPicture
     * @return
     */
    @AutoLog(value = "轮播图-编辑")
    @RequiresPermissions("control:picture:edit")
    @PostMapping(value = "/edit")
    @ResponseBody
    public Result<?> edit(@Validated WxPicture wxPicture, String photo) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();//取得当前用户
        wxPicture.setUpdateBy(user.getRealname()).setUpdateTime(new Date());
        String id = wxPicture.getId();
        if(picturePhotoService.count(id) > 0){
            picturePhotoService.update(id,photo);
        }else {
            picturePhotoService.addPhoto(id,photo);
        }
        wxPictureService.updateById(wxPicture);
        return Result.ok("编辑成功!");
    }

}
